using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ALib;
using AxTWSLib;
using TWSLib;
using _DTwsEvents_historicalDataEventHandler = AxTWSLib._DTwsEvents_historicalDataEventHandler;
using _DTwsEvents_updateAccountValueEventHandler=AxTWSLib._DTwsEvents_updateAccountValueEventHandler;

namespace TradingPlatform.IB.Operations
{
    public class AccountUpdates
    {
        private readonly AxTws _tws;

        public readonly List<string> _result;

        public AccountUpdates(AxTws tws)
        {
            _tws = tws;
            //_tws.updateAccountTime += new AxTWSLib._DTwsEvents_updateAccountTimeEventHandler(_tws_updateAccountTime);
            //_tws.updateAccountValue += new _DTwsEvents_updateAccountValueEventHandler(_tws_updateAccountValue);
            _tws.updatePortfolioEx += new AxTWSLib._DTwsEvents_updatePortfolioExEventHandler(_tws_updatePortfolioEx);
            _result = new List<string>();
        }

        public void RequestAccountUpdates(string accountCode)
        {
            _result.Clear();
            string items = string.Format(@"{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}", "accountName",
                                         "contract.primaryExchange", "contract.secType", "contract.symbol", "contract.expiry", "contract.currency",
                                         "averageCost",
                                         "marketPrice",
                                         "marketValue",
                                         "position",
                                         "realizedPNL",
                                         "unrealizedPNL");
            _result.Add(items);

            _tws.reqAccountUpdates(1, accountCode);
        }

        //void _tws_updateAccountTime(object sender, _DTwsEvents_updateAccountTimeEvent e)
        //{
        //    string s = e.timeStamp;
        //}

        //void _tws_updateAccountValue(object sender, _DTwsEvents_updateAccountValueEvent e)
        //{
        //    string s = e.accountName;
        //}
        
        void _tws_updatePortfolioEx(object sender, _DTwsEvents_updatePortfolioExEvent e)
        {
            // string exchange, string secType, string symbol, string expiry, string currency,
            string contractInfo = string.Format(@"{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}", e.accountName,
                                                e.contract.primaryExchange, e.contract.secType, e.contract.symbol, e.contract.expiry, e.contract.currency,
                                                e.averageCost,
                                                e.marketPrice,
                                                e.marketValue,
                                                e.position,
                                                e.realizedPNL,
                                                e.unrealizedPNL == double.MaxValue ? 0 : e.unrealizedPNL);
            _result.Add(contractInfo);
        }
    }
}